/*	This is for me to learn native Xorg programming. Gotta do everything
	from the ground up. See how hard it was for me?
*/

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

typedef struct task
{
	struct task *next;
	Window win;
	Pixmap icon;
	Pixmap mask;
	char *name;
	int pos_x;
	int width;
	unsigned int focused:1;
	unsigned int iconified:1;
	unsigned int icon_copied:1;
}
task;

Display *dd;
Window root_win;
int scr_screen;
char *atom_names[] = {
	"KWM_WIN_ICON",
	"_MOTIF_WM_HINTS",
	"_WIN_WORKSPACE",
	"_WIN_HINTS",
	"_WIN_LAYER",
	"_NET_CLIENT_LIST",
	"_WIN_CLIENT_LIST",
	"_WIN_WORKSPACE_COUNT",
	"_WIN_STATE",
	"WM_STATE"
};

#define ATOM_COUNT (sizeof (atom_names) / sizeof (atom_names[0]))

Atom atoms[ATOM_COUNT];

#define atom_KWM_WIN_ICON atoms[0]
#define atom__MOTIF_WM_HINTS atoms[1]
#define atom__WIN_WORKSPACE atoms[2]
#define atom__WIN_HINTS atoms[3]
#define atom__WIN_LAYER atoms[4]
#define atom__NET_CLIENT_LIST atoms[5]
#define atom__WIN_CLIENT_LIST atoms[6]
#define atom__WIN_WORKSPACE_COUNT atoms[7]
#define atom__WIN_STATE atoms[8]
#define atom_WM_STATE atoms[9]


void *
get_prop_data (Window win, Atom prop, Atom type, int *items)
{
	Atom type_ret;
	int format_ret;
	unsigned long items_ret;
	unsigned long after_ret;
	unsigned char *prop_data;

	prop_data = 0;

	XGetWindowProperty (dd, win, prop, 0, 0x7fffffff, False,
							  type, &type_ret, &format_ret, &items_ret,
							  &after_ret, &prop_data);
	warn ("One" ) ;
	if (items)
		*items = items_ret;

	return prop_data;
}

int main ( int argc, char ** argv )
{
	Window *win, focus_win;
	int num, i, rev, desk, new_desk = 0;
	task *list, *next;

	dd = XOpenDisplay ( NULL ) ;
	if (!dd)
		return 0;

	scr_screen = DefaultScreen (dd);
	root_win = RootWindow (dd, scr_screen);

	XSelectInput (dd, root_win, PropertyChangeMask);
	XInternAtoms (dd, atom_names, ATOM_COUNT, False, atoms);
		
	XSync (dd, False);
	XGetInputFocus (dd, &focus_win, &rev);
	
	/* try unified window spec first */
	win = get_prop_data (root_win, atom__NET_CLIENT_LIST, XA_WINDOW, &num);
	if (!win)
	{
		/* failed, let's try gnome */
		win = get_prop_data (root_win, atom__WIN_CLIENT_LIST, XA_CARDINAL, &num);
		if (!win)
			return;
	}

	warn ("two");
	
	
	for ( i = 0 ; i < num ; i ++ )
		printf ("%s\n", get_prop_data (root_win, XA_WM_NAME, XA_STRING, 0));

	
}
